<?php
namespace jy_cun9fan;
defined('BY_JYA') or exit('error');
/*
	分红佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		$is_open=intval($this->setting['is_open']);
		if($is_open==0){
			return;
		}
		$this->order=$order;
		$member=pdo_get('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		$member['mid']=$member['id'];
		$fan_member=pdo_get('jy_cun9fan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		$fan_member['agent_id']=$member['fromuser'];
		$member=array_merge($member,$fan_member);
		if($member['level_id']){
			$this->nowlevel=$this->getcolumn('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']),'level');
		}else{
			$this->nowlevel=0;
		}
		$this->up_fanred($member);
		//自购升级
		if($member['is_agent']){
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
		}
		if($this->order['fee']<=0){
			return;
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'agent_id'=>$member['agent_id']?:$this->order['mid'],
				'ordersn'=>$this->order['id'],
				'price'=>$this->order['fee'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_price'=>$this->order['jifenfee']?:$this->order['fee'],
				'commission_money'=>0,
				'fee2'=>0,
				'ispay'=>1,
				'nofeemid'=>';',
				'createtime'=>TIMESTAMP,
		);
		$agent_id=$member['agent_id'];
		for($i=0;$i<58;$i++){
			if(!$agent_id){
				break;
			}
			$member=pdo_get('core_members',array('id'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
			$member['mid']=$member['id'];
			$this->model('member')->update_agent_level($member['mid']);
			$fan_member=pdo_get('jy_cun9fan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
			$fan_member['agent_id']=$member['fromuser'];
			$member=array_merge($member,$fan_member);
			$agent_id=$member['fromuser'];
			$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			if($this->setting['is_feng']){
				$isfanin=pdo_getcolumn('jy_cun9fan_fanlog',array('status'=>array(0,2),'uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']),'id');
			}
			if($isfanin || !$this->setting['is_feng']){
				$this->create_commission($member,$i+1,$level);
			}else{
				$this->commission_order['nofeemid'].=$member['mid'].';';
			}
			$this->nowlevel=$level['level'];
			//如果他的上级是我
			$ta_agent_id=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['fromuser']),'fromuser');
			if(($agent_id>0 && $ta_agent_id==$agent_id) || $agent_id==$member['mid']){
				break;
			}
		}
		//生成分红订单
		$indata=$this->commission_order;
		unset($indata['fee2']);
		$this->insert('order',$indata);
		return true;
	}
	function create_commission($member,$commission_level=1,$level=array(),$isfee3=0,$order=array(),$pinglevel_id=0,$nowfee3=0,$isup=0){
		if($isfee3){
			$this->pinglevel_id=$pinglevel_id;
			$this->nowfee3=$nowfee3;
			$this->order=$order;
			$this->commission_order['fee2']=$nowfee3;
		}
		$nowfee3=$this->nowfee3;
		if($member && $member['is_agent'] && $member['stop']==0){
			//相应会员存在且是分销商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
				$level=$this->plugin_setting('default_level');
				$level['level']=0;
				$level['id']=0;
				$level_id=0;
			}else{
				$level_id=$level['id'];
			}
			if($level['id']==$this->pinglevel_id && $this->pinglevel_id){
				$isping=1;
			}else{
				$isping=0;
			}
			//file_put_contents(ROOT_D.'/1.txt','~isping~'.$isping);
			$fee2=0;
			$fee3=0;
			$fee4=0;
			$order_price=$this->commission_order['commission_price'];
			$per2=$level['red_rate'];
			$per3=$level['ping_rate'];
			$per4=$level['jia_rate'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			if(substr($per2,-1)=='%'){
				$fee2=intval($order_price*substr($per2,0,-1))/100;
				$fee2 -=$this->commission_order['fee2'];
			}else{
				$fee2=$per2;
				$fee2 -=$this->commission_order['fee2'];
			}
			//file_put_contents(ROOT_D.'/1.txt',$level['id'].'=='.$this->pinglevel_id.'~isping~'.$isping.'~mid~'.$member['mid'].'~$fee2~'.$fee2.'~$per3~'.$per3.'~$fee3~'.$fee3."\n",FILE_APPEND);
			if($isping){
				if(substr($per3,-1)=='%'){
					$fee3=intval($nowfee3*substr($per3,0,-1))/100;
				}else{
					$fee3=$per3;
				}
			}
			if($per4 && ($commission_level==1 || $commission_level==2)){
				$isbuy=pdo_get('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$this->order['mid'],'status'=>1));
				if($isbuy['id']){
					if(substr($per4,-1)=='%'){
						$fee4=intval($order_price*substr($per4,0,-1))/100;
					}else{
						$fee4=$per4;
					}
				}
			}
			if($fee3>0 && !$isfee3){
				$this->commission_order['commission_money']+=$fee3;
				$remark['commission_money']=$fee3;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'level_id'=>$this->pinglevel_id,
						'ordersn'=>$this->order['id'],
						'money'=>$fee3,
						'type'=>3,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>2,
				);
				$this->insert('commission',$new_commission);
				cfc('core')->member_credit($member['mid'],'credit2',$fee3,1,'jy_cun9fan','平级奖');
				$this->pinglevel_id=0;
				$this->nowfee3=0;
			}elseif($fee3>0 && $isfee3){
				$isadd=$this->get('commission',array('level_id'=>$this->pinglevel_id,'uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'ordersn'=>$this->order['id']));
				if($isadd['id']){
					exi('该上级的平级之前已产生过！','error');
				}elseif($isup){
					$remark['commission_money']=$fee3;
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$member['mid'],
							'level'=>$commission_level,
							'level_id'=>$this->pinglevel_id,
							'ordersn'=>$this->order['id'],
							'money'=>$fee3,
							'type'=>3,
							'remark'=>ijson_encode($remark),
							'createtime'=>TIMESTAMP,
							'status'=>2,
					);
					$this->insert('commission',$new_commission);
					cfc('core')->member_credit($member['mid'],'credit2',$fee3,1,'jy_cun9fan','平级奖');
				}
				return $fee3;
			}
			if($fee2>0 && !$isfee3){
				$this->commission_order['fee2']+=$fee2;
				$this->commission_order['commission_money']+=$fee2;
				$remark['commission_money']=$fee2;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['id'],
						'money'=>$fee2,
						'type'=>2,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>2,
				);
				$this->insert('commission',$new_commission);
				cfc('core')->member_credit($member['mid'],'credit2',$fee2,1,'jy_cun9fan','销售收益');
				$this->pinglevel_id=$level_id;
				$this->nowfee3=$fee2;
			}elseif($fee2>0 && $isfee3){
				return -1;
			}
			if($fee4>0 && !$isfee3){
				$this->commission_order['commission_money']+=$fee4;
				$remark['commission_money']=$fee4;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['id'],
						'money'=>$fee4,
						'type'=>4,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>1,
				);
				$this->insert('commission',$new_commission);
				cfc('core')->member_credit($member['mid'],'credit2',$fee4,1,'jy_cun9fan','家族奖');
			}
		}
		if($isfee3){
			return 0;
		}else{
			return $member['agent_id'];
		}
	}
	public function fee3check($fee2item=array(),$isup=0){
		if($fee2item['type']!=2){
			exi('不是销售奖，不能产生平级！','error');
		}
		$order=pdo_get('core_recharge',array('uniacid'=>$_SESSION['uniacid'],'id'=>$fee2item['ordersn']));
		$agent_id=pdo_getcolumn('core_members',array('id'=>$fee2item['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$level_id=pdo_getcolumn('jy_cun9fan_member',array('mid'=>$fee2item['mid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
		for($i=0;$i<58;$i++){
			if(!$agent_id){
				break;
			}
			$member=pdo_get('core_members',array('id'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
			$member['mid']=$member['id'];
			$this->model('member')->update_agent_level($member['mid']);
			$fan_member=pdo_get('jy_cun9fan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
			$fan_member['agent_id']=$member['fromuser'];
			$member=array_merge($member,$fan_member);
			$agent_id=$member['fromuser'];
			$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$fee3=$this->create_commission($member,$i+1,$level,1,$order,$level_id,$fee2item['money'],$isup);
			if($fee3){
				if($isup && $fee3<0){
					exi('不可产生平级奖，上级已有销售奖抵冲了','error');
				}else{
					$msg=$fee3>0?'可产生平级奖'.$fee3.'元':'不可产生平级奖，上级已有销售奖抵冲了！';
				}
				return $msg;
				break;
			}
			//如果他的上级是我
			$ta_agent_id=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['fromuser']),'fromuser');
			if(($agent_id>0 && $ta_agent_id==$agent_id) || $agent_id==$member['mid']){
				break;
			}
		}
		if($isup){
			exi('不满足产生平级奖的条件','error');
		}
		return '不满足产生平级奖的条件';
	}
	public function up_fanred($member=array()){
		$redfeeset=$this->setting['redfeeset'];
		foreach($redfeeset as $rv){
			if($rv['fee']==$this->order['fee']){
				$fanfee=$rv['fanfee'];
				$jiafee=$rv['jiafee'];
				$bei=$rv['bei']?:1;
				break;
			}
		}
		//$onefanfee=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>1),'money');
		if($fanfee){
			$fanday=intval($this->setting['fanday']);
			$fandaytime=24*3600*($fanday-1);
			$fandaytime=$fandaytime>0?$fandaytime:0;
			$lastfanlog=pdo_get('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'','id desc');
			$addlastfanfee=$lastfanlog['lastfanfee'];
			$fanlog=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'orderid'=>$this->order['id'],
				'bei'=>$bei,
				'fanfee'=>$fanfee,
				'lastfanfee'=>$fanfee+$addlastfanfee,
				'addlastfanfee'=>$addlastfanfee,
				'createtime'=>TIMESTAMP,
				'uptime'=>TIMESTAMP+$fandaytime,
			);
			$this->insert('fanlog',$fanlog);
			$fanlog['id']=pdo_insertid();
			if($member['agent_id']){
				$this->up_fee1($fanlog,$jiafee,$member['agent_id']);
			}
			if(!$this->setting['fanday']){
				$this->up_fanone($fanlog);
			}
		}
		return true;
	}
	public function up_fee1($fanlog=array(),$jiafee=0,$agent_id=0){
		if($jiafee<=0){
			return false;
		}
		if($this->setting['is_feng']){
			$isfanin=pdo_getcolumn('jy_cun9fan_fanlog',array('status'=>array(0,2),'uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id),'id');
		}
		if(!$isfanin && $this->setting['is_feng']){
			return false;		
		}
		$new_commission=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$agent_id,
			'level'=>1,
			'ordersn'=>$fanlog['orderid'],
			'money'=>$jiafee,
			'type'=>1,
			'createtime'=>TIMESTAMP,
			'status'=>2,
		);
		$this->insert('commission',$new_commission);
		pdo_update('jy_cun9fan_member',array('jiafee +='=>$jiafee,'lastjiafee +='=>$jiafee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
		$fanlog=pdo_get('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id,'status'=>2));
		//$this->up_fanone($fanlog,'',$jiafee);
		return true;
	}
	public function up_fanone($fanlog=array(),$fandayfee=0,$tojiafee=0,$isshou=0){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		if(!$fanlog['isfandayfee'] && $tojiafee){
			if($this->setting['dayallfeetype']){
				$dayallfee=$this->setting['dayallfee'];
			}else{
				$lastdaytime=$daytime-24*3600;
				$dayallfee=pdo_getsum('core_recharge',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'pluginname'=>'jy_cun9fan','createtime between'=>array($lastdaytime,$daytime)),'fee');
			}
			$fannum = pdo_count('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'lastfanfee >'=>0,'uptime <'=>$daytime));
			if($fannum>0){
				$bei=$fanlog['bei']?:1;
				if(!$fanlog['bei']){
					$redfeeset=$this->setting['redfeeset'];
					foreach($redfeeset as $rv){
						if($rv['fanfee']==$fanlog['fanfee']){
							$bei=$rv['bei']?:1;
							if($rv['bei']){
								pdo_update('jy_cun9fan_fanlog',array('bei'=>$bei),array('id'=>$fanlog['id'],'uniacid'=>$_SESSION['uniacid']));
							}
							break;
						}
					}
				}
				$fandayfee=$bei*$dayallfee*$this->setting['fandayfee']/(100*$fannum);
			}
		}
		if(!$tojiafee){
			$member=pdo_get('jy_cun9fan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fanlog['mid']));
			if($member['lastjiafee']>0){
				$this->up_fanone($fanlog,'',$member['lastjiafee']);
				$fanlog=pdo_get('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$fanlog['id']));
				if($fanlog['status']==1){
					return false;
				}
			}
			//$jiafee=$this->getcolumn('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fanlog['mid']),'jiafee');
			//$jiafee=$jiafee?:0;
			$fandayrate=$fanlog['fandayrate']>0?$fanlog['fandayrate']:$this->setting['fandayrate'];
			$fandayrate=$fandayrate?:0;
			$maxfandayfee=$fanlog['fanfee']*$fandayrate/100;
			if(($maxfandayfee && $maxfandayfee<$fandayfee) || !$fandayfee){
				$fandayfee=$maxfandayfee;
			}
		}else{
			$fandayfee=$tojiafee>$fanlog['lastfanfee']?$fanlog['lastfanfee']:$tojiafee;
		}
		if(!$fandayfee){
			if($isshou){
				exi('分润额度不正确！','error');
			}
			return false;
		}
		if($fandayfee>$fanlog['lastfanfee']){
			//不足反补，推给下次投资
			if($isshou){
				exi('不足分润，推给下次投资！','error');
			}
			$fandayfee=$fanlog['lastfanfee'];
			//pdo_update('jy_cun9fan_fanlog',array('status'=>1),array('id'=>$fanlog['id'],'uniacid'=>$_SESSION['uniacid']));
			//return false;
		}
		if(!$tojiafee && $fanlog['uptime']>$daytime){
			return false;
		}
		$new_commission=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$fanlog['mid'],
			'level'=>0,
			'ordersn'=>$fanlog['orderid'],
			'money'=>$fandayfee,
			'type'=>$tojiafee?7:6,
			'createtime'=>TIMESTAMP,
			'status'=>4,
		);
		if($this->setting['is_redopen']){
			$new_commission['status']=3;
		}else{
			$new_commission['complete_time']=TIMESTAMP;
		}
		if($isshou){
			$new_commission['type']=6;
		}
		$this->insert('commission',$new_commission);
		if($this->setting['is_redopen']!=1){
			$title=$tojiafee?'加速奖':'充值周期返额';
			if($isshou){
				$title='充值周期返额';
			}
			cfc('core')->member_credit($fanlog['mid'],'credit2',$fandayfee,1,'jy_cun9fan',$title);
		}
		if($fanlog['id']){
			if($tojiafee){
				$update=array();
			}else{
				$update=array('status'=>2,'uptime'=>TIMESTAMP);
			}
			if($fandayfee==$fanlog['lastfanfee']){
				$update['status']=1;
				$update['lastfanfee']=0;
			}else{
				$update['lastfanfee -=']=$fandayfee;
			}
			pdo_update('jy_cun9fan_fanlog',$update,array('id'=>$fanlog['id'],'uniacid'=>$_SESSION['uniacid']));
			if($tojiafee && !$isshou){
				//file_put_contents(ROOT_D.'/1.TXT',$fandayfee.'~~'.$fanlog['mid']);
				pdo_update('jy_cun9fan_member',array('lastjiafee -='=>$fandayfee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fanlog['mid']));
			}
			if($update['status']==1){
				$this->model('notice')->notice_chuju($fanlog);
			}
		}
		return true;
	}
}